<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag type="list"  v-model="model.classId"  dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc" placeholder="请选择班级"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="巡检日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectionDate">
              <j-date placeholder="请选择巡检日期" v-model="model.inspectionDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="巡检次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectionNum">
              <a-input-number v-model="model.inspectionNum" placeholder="请输入巡检次数" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="巡检时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectionTime">
              <j-time placeholder="请选择巡检时间"  v-model="model.inspectionTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="环节重点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectionPoint">
              <a-textarea v-model="model.inspectionPoint" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入环节重点"  />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.inspectionPoint?model.inspectionPoint.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="情况记录" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comment">
              <a-textarea v-model="model.comment" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入情况记录" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.comment?model.comment.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="巡检人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="staffId">
              <a-select v-model="model.staffId" placeholder="请选择巡检人" allowClear>
                <a-select-option v-for="(item, key) in dictOptions" :key="key" :value="item.id">
                  <span style="display: inline-block;width: 100%" :title=" item.realname ">
                    {{ item.realname }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SkHealthInspectionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           inspectionDate: [
              { required: true, message: '请输入巡检日期!'},
           ],
           inspectionNum: [
              { required: true, message: '请输入巡检次数!'},
           ],
           inspectionTime: [
              { required: true, message: '请输入巡检时间!'},
           ],
           staffId: [
              { required: true, message: '请输入巡检人!'},
           ],
        },
        dictOptions: [],
        url: {
          add: "/health/skHealthInspection/add",
          edit: "/health/skHealthInspection/edit",
          queryById: "/health/skHealthInspection/queryById",
          checkList: "/management/teacher/list"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getDicts();
    },
    methods: {
      getDicts() {
        getAction(this.url.checkList, []).then(res => {
          if(res.success) {
            this.dictOptions = res.result
          }else{
            that.$message.warning(res.message);
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>